﻿using System;
using System.Collections.Generic;
using ZhCun.DbCore;
using ZhCun.DbCore.BuildSQLText;
using ZhCun.DbCore.Cores;
using ZhCun.Utils.Helpers;
using ZhCun.WebCommon.DBModels;

namespace ZhCun.WebCommon.AL
{
    public class BaseDBContext : DBContextDebug //DBContext
    {
        public BaseDBContext(EmDbType dbType, string connStr)
            : base(dbType, connStr)
        { }
        /// <summary>
        /// 是否使用用户权限
        /// </summary>
        public bool UseDataRight { private set; get; }
        /// <summary>
        /// 登录用户
        /// </summary>
        public TSysLoginToken LoginUser { private set; get; }

        public string LoginHost { private set; get; }

        /// <summary>
        /// 获取当前时间
        /// </summary>
        public DateTime GetNowTime()
        {
            //取数据库时间可用: select getdate()
            return DateTime.Now;
        }
        /// <summary>
        /// 设置登录状态对象
        /// </summary>
        public void SetLoginUser(TSysLoginToken loginUser, bool useDataRight)
        {
            LoginUser = loginUser;
            UseDataRight = useDataRight;

        }

        public void SetLoginHost(string loginHost)
        {
            LoginHost = loginHost;
        }

        //public override ExecResult ExecSql(ISqlBuilder sqlBuilder)
        //{
        //    //暂且不做重新，有具体需求后看DebugDBContent
        //    return base.ExecSql(sqlBuilder);
        //}
        protected override void ResultFinish(BaseResult result)
        {
            //TODO:这里可以记录sql日志
        }
        public override ExecResult Insert<TEntity>(TEntity entity)
        {
            if (LoginHost != null)
            {
                ReflectionHelper.SetPropertyValue(entity, AddPC, LoginHost);
            }
            if (LoginUser != null)
            {
                ReflectionHelper.SetPropertyValue(entity, AddUserId, LoginUser.LoginUserId);
                //数据库已赋值则不用
                //ReflectionHelper.SetPropertyValue(entity, AddTime, GetNowTime()); 
            }
            return base.Insert<TEntity>(entity);
        }
        protected override ExecResult UpdateBase<TEntity>(TEntity entity, Func<ISqlBuilder, string> whereFun)
        {
            if (LoginHost != null)
            {
                ReflectionHelper.SetPropertyValue(entity, LastPC, LoginHost);
            }
            if (LoginUser != null)
            {
                ReflectionHelper.SetPropertyValue(entity, LastUserId, LoginUser.LoginUserId);                
            }
            ReflectionHelper.SetPropertyValue(entity, LastTime, GetNowTime());
            return base.UpdateBase<TEntity>(entity, whereFun);
        }
        public override ExecResult Delete<TEntity>(ICondition execCondition)
        {
            var deleteResult = base.Delete<TEntity>(execCondition);
            // deleteResult.SQL;
            //TODO:删除操作记录日志
            return deleteResult;
        }
        protected override void BeforeExecSqlBuilder<TEntity>(ISqlBuilder sqlBuilder, EmDbOperation opType)
        {
            if (UseDataRight == false || LoginUser == null) return;
            if (QueryFilterHelper.IsFilter())
            {
                DataRightsHelper.DataRightsHandler<TEntity>(sqlBuilder, opType, LoginUser);
            }
        }
        public override ProcResult ExecProcedure<ProcEntity>(ProcEntity procObj)
        {
            if (LoginHost != null)
            {
                ReflectionHelper.SetPropertyValue(procObj, P_PARAM_HOST, LoginHost);
            }
            if (LoginUser != null)
            {
                ReflectionHelper.SetPropertyValue(procObj, P_PARAM_USER, LoginUser.LoginUserId);                
            }
            return base.ExecProcedure<ProcEntity>(procObj);
        }

        const string AddUserId = "AddUserId";
        //const string AddTime = "AddTime";
        const string AddPC = "AddHost";
        const string LastUserId = "LastUserId";
        const string LastPC = "LastHost";
        const string LastTime = "LastTime";
        /// <summary>
        /// 存储过程登录用户Id的参数名
        /// </summary>
        const string P_PARAM_USER = "Arg_LoginUserId";
        /// <summary>
        /// 存储过程登录的地址的参数名
        /// </summary>
        const string P_PARAM_HOST = "Arg_LoginHost";
    }
}
